﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Linq;

namespace WinForm_LogSystem
{
    public class LogSystem_RichTextBox
    {
        /// <summary>
        /// 日志输出源
        /// </summary>
        public RichTextBox richTextBox_Log;

        /// <summary>
        /// 日志文件夹名字
        /// </summary>
        public string folderName;

        /// <summary>
        /// 日志保存数
        /// </summary>
        public int saveCount;
        /// <summary>
        /// 构建一个实例日志系统
        /// </summary>
        /// <param name="richTextBox">输出源</param>
        /// <param name="saveCount">日志保存数量</param>
        /// <param name="folderName">日志文件夹</param>
        public LogSystem_RichTextBox(RichTextBox richTextBox, int saveCount = 5, string folderName = "RunLog")
        {
            this.richTextBox_Log = richTextBox;
            this.saveCount = saveCount;
        }
        /// <summary>
        /// 输入日志到输出源
        /// </summary>
        /// <param name="text">日志内容</param>
        /// <param name="state">日志内容级别</param>
        public void WriteLog(string text, LogState state = LogState.Normal)
        {
            string time = DateTime.Now.ToString("T");//日志时间
            int start = richTextBox_Log.TextLength;
            
            richTextBox_Log.AppendText("[" + time + "][" + state.ToString() + "]:");
            richTextBox_Log.AppendText(text + "\n");
            int length = time.Length + text.Length + 6 + state.ToString().Length;
            richTextBox_Log.Select(start, length);
            switch (state)
            {
                case LogState.Normal:richTextBox_Log.SelectionColor = stateColor_Normal;break;
                case LogState.Warning: richTextBox_Log.SelectionColor = stateColor_Warning; break;
                case LogState.Good: richTextBox_Log.SelectionColor = stateColor_Good; break;
                case LogState.FatalError: richTextBox_Log.SelectionColor = stateColor_FatalError; break;
            }
        }
        /// <summary>
        /// 输入日志到输出源
        /// </summary>
        /// <param name="args">日志参数</param>
        public void WriteLog(LogArgs args)
        {
            WriteLog(args.log, args.state);
        }
        /// <summary>
        /// 供事件调用的WriteLog函数重载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void WriteLog(object? sender,LogArgs args)
        {
            WriteLog(args.log, args.state);
        }
        /// <summary>
        /// 保存日志文件
        /// </summary>
        public void SaveLog()
        {
            //检查日志文件夹是否存在，不存在则创立
            if (!Directory.Exists(folderName)) Directory.CreateDirectory(folderName);
            #region 多余日志清除功能
            string[] fileNames = Directory.GetFiles(folderName);
            int alreadySaveCount = fileNames.Length;
            if (alreadySaveCount >= saveCount)
            {
                var sortedFileNames = from a in fileNames orderby File.GetCreationTime(a) select a;
                int count = 0;
                foreach (var file in sortedFileNames)
                {
                    File.Delete(file);
                    if (count++ == (alreadySaveCount - saveCount)) break;
                }
            }
            #endregion 多余日志清除功能
            //新日志写入
            string fileName = folderName + "\\" + DateTime.Now.ToString("G") + ".txt";
            File.WriteAllText(fileName, richTextBox_Log.Text);

        }
        #region 日志显示颜色定义
        static Color stateColor_Normal = Color.Black;
        static Color stateColor_Good = Color.Green;
        static Color stateColor_Warning = Color.FromArgb(255, 166, 0);
        static Color stateColor_FatalError = Color.Red;
        #endregion 日志显示颜色定义
    }

    /// <summary>
    /// 用于简化代码的输出日志的委托
    /// </summary>
    /// <param name="text">日志内容</param>
    /// <param name="state">日志内容级别</param>
    public delegate void WRITELOG(string text, LogState state = LogState.Normal);
}
